

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>argoverse.data_loading.trajectory_loader &mdash; argoverse  documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> argoverse
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">argoverse</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>argoverse.data_loading.trajectory_loader</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for argoverse.data_loading.trajectory_loader</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>

<span class="c1"># &lt;Copyright 2019, Argo AI, LLC. Released under the MIT license.&gt;</span>

<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">List</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.object_classes</span> <span class="k">import</span> <span class="n">OBJ_CLASS_MAPPING_DICT</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="TrajectoryLabel"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.trajectory_loader.TrajectoryLabel">[docs]</a><span class="k">class</span> <span class="nc">TrajectoryLabel</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Trajectory object.</span>

<span class="sd">    Args:</span>
<span class="sd">        timestamps (np.array): Array of timestamps for trajectory.</span>
<span class="sd">        quaternions (np.array): Array of quaternions for trajectory.</span>
<span class="sd">        translations (np.array): Array of translations of SE3 poses for trajectory.</span>
<span class="sd">        obj_class (int): Object class id.</span>
<span class="sd">        obj_class_str (str): Object class name.</span>
<span class="sd">        occlusions (np.array): Array of occlusions for trajectory.</span>
<span class="sd">        track_uuid (str): Track uuid.</span>
<span class="sd">        log_id (str): Log id.</span>
<span class="sd">        max_length (float): Maximum length for trajectory.</span>
<span class="sd">        max_width (float): Maximum width for trajectory.</span>
<span class="sd">        max_height (float): Maximum height for trajectory.</span>
<span class="sd">        lengths (np.array): Array of lengths for trajectory.</span>
<span class="sd">        widths (np.array): Array of widths for trajectory.</span>
<span class="sd">        heights (np.array): Array of heights for trajectory.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="TrajectoryLabel.__init__"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.trajectory_loader.TrajectoryLabel.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">timestamps</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">quaternions</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">translations</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">obj_class</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
        <span class="n">obj_class_str</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">occlusions</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">track_uuid</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">log_id</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">max_length</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">max_width</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">max_height</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">lengths</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">widths</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
        <span class="n">heights</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Initialize TrajectoryLabel object.</span>

<span class="sd">        Args:</span>
<span class="sd">            timestamps (np.array): Array of timestamps for trajectory.</span>
<span class="sd">            quaternions (np.array): Array of quaternions for trajectory.</span>
<span class="sd">            translations (np.array): Array of translations of SE3 poses for trajectory.</span>
<span class="sd">            obj_class (int): Object class id.</span>
<span class="sd">            obj_class_str (str): Object class name.</span>
<span class="sd">            occlusions (np.array): Array of occlusions for trajectory.</span>
<span class="sd">            track_uuid (str): Track uuid.</span>
<span class="sd">            log_id (str): Log id.</span>
<span class="sd">            max_length (float): Maximum length for trajectory.</span>
<span class="sd">            max_width (float): Maximum width for trajectory.</span>
<span class="sd">            max_height (float): Maximum height for trajectory.</span>
<span class="sd">            lengths (np.array): Array of lengths for trajectory.</span>
<span class="sd">            widths (np.array): Array of widths for trajectory.</span>
<span class="sd">            heights (np.array): Array of heights for trajectory.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timestamps</span> <span class="o">=</span> <span class="n">timestamps</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quaternions</span> <span class="o">=</span> <span class="n">quaternions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">translations</span> <span class="o">=</span> <span class="n">translations</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obj_class</span> <span class="o">=</span> <span class="n">obj_class</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obj_class_str</span> <span class="o">=</span> <span class="n">obj_class_str</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">occlusion</span> <span class="o">=</span> <span class="n">occlusions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">track_uuid</span> <span class="o">=</span> <span class="n">track_uuid</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">log_id</span> <span class="o">=</span> <span class="n">log_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_length</span> <span class="o">=</span> <span class="n">max_length</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_width</span> <span class="o">=</span> <span class="n">max_width</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_height</span> <span class="o">=</span> <span class="n">max_height</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lengths</span> <span class="o">=</span> <span class="n">lengths</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widths</span> <span class="o">=</span> <span class="n">widths</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">heights</span> <span class="o">=</span> <span class="n">heights</span></div></div>


<div class="viewcode-block" id="load_json_track_labels"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.trajectory_loader.load_json_track_labels">[docs]</a><span class="k">def</span> <span class="nf">load_json_track_labels</span><span class="p">(</span><span class="n">log_track_labels_dir</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">TrajectoryLabel</span><span class="p">]:</span>
    <span class="sd">&quot;&quot;&quot;Trajectories are stored on disk as 1 track per 1 json file.</span>
<span class="sd">     We load all labeled tracks here from the JSON files.</span>

<span class="sd">    Args:</span>
<span class="sd">        log_track_labels_dir (str): Log track directory.</span>

<span class="sd">    Returns:</span>
<span class="sd">        List[TrajectoryLabel]: a Python list of TrajectoryLabels.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">json_fpath_list</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">log_track_labels_dir</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">trajectories</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">for</span> <span class="n">file_idx</span><span class="p">,</span> <span class="n">json_fpath</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">json_fpath_list</span><span class="p">):</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">json_fpath</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">json_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="n">track_uuid</span> <span class="o">=</span> <span class="n">json_fpath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">obj_cls</span> <span class="o">=</span> <span class="n">json_data</span><span class="p">[</span><span class="s2">&quot;label_class&quot;</span><span class="p">]</span>

        <span class="c1"># recent MLDS change</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj_cls</span><span class="p">,</span> <span class="n">collections</span><span class="o">.</span><span class="n">abc</span><span class="o">.</span><span class="n">Mapping</span><span class="p">):</span>
            <span class="n">obj_cls</span> <span class="o">=</span> <span class="n">obj_cls</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">obj_cls</span> <span class="ow">in</span> <span class="n">OBJ_CLASS_MAPPING_DICT</span><span class="p">:</span>
            <span class="n">obj_cls_idx</span> <span class="o">=</span> <span class="n">OBJ_CLASS_MAPPING_DICT</span><span class="p">[</span><span class="n">obj_cls</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Unrecognized class </span><span class="si">{obj_cls}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Unrecognized class </span><span class="si">{obj_cls}</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="n">quaternions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">translations</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">timestamps</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">occlusions</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">lengths</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">widths</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">heights</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">track_frame</span> <span class="ow">in</span> <span class="n">json_data</span><span class="p">[</span><span class="s2">&quot;track_label_frames&quot;</span><span class="p">]:</span>

            <span class="c1"># cuboid not occluded if not explicitly indicated.</span>
            <span class="n">occlusions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">track_frame</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;occlusion&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
            <span class="n">tr_center</span> <span class="o">=</span> <span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;center&quot;</span><span class="p">]</span>
            <span class="n">tr_x</span> <span class="o">=</span> <span class="n">tr_center</span><span class="p">[</span><span class="s2">&quot;x&quot;</span><span class="p">]</span>
            <span class="n">tr_y</span> <span class="o">=</span> <span class="n">tr_center</span><span class="p">[</span><span class="s2">&quot;y&quot;</span><span class="p">]</span>
            <span class="n">tr_z</span> <span class="o">=</span> <span class="n">tr_center</span><span class="p">[</span><span class="s2">&quot;z&quot;</span><span class="p">]</span>

            <span class="n">translation</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">tr_x</span><span class="p">,</span> <span class="n">tr_y</span><span class="p">,</span> <span class="n">tr_z</span><span class="p">])</span>
            <span class="n">translations</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">translation</span><span class="p">)</span>

            <span class="n">tr_rot</span> <span class="o">=</span> <span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;rotation&quot;</span><span class="p">]</span>
            <span class="n">rot_w</span> <span class="o">=</span> <span class="n">tr_rot</span><span class="p">[</span><span class="s2">&quot;w&quot;</span><span class="p">]</span>
            <span class="n">rot_x</span> <span class="o">=</span> <span class="n">tr_rot</span><span class="p">[</span><span class="s2">&quot;x&quot;</span><span class="p">]</span>
            <span class="n">rot_y</span> <span class="o">=</span> <span class="n">tr_rot</span><span class="p">[</span><span class="s2">&quot;y&quot;</span><span class="p">]</span>
            <span class="n">rot_z</span> <span class="o">=</span> <span class="n">tr_rot</span><span class="p">[</span><span class="s2">&quot;z&quot;</span><span class="p">]</span>

            <span class="n">quaternion</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">rot_w</span><span class="p">,</span> <span class="n">rot_x</span><span class="p">,</span> <span class="n">rot_y</span><span class="p">,</span> <span class="n">rot_z</span><span class="p">])</span>
            <span class="n">quaternions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">quaternion</span><span class="p">)</span>

            <span class="n">timestamps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;timestamp&quot;</span><span class="p">])</span>

            <span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;length&quot;</span><span class="p">])</span>
            <span class="n">widths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;width&quot;</span><span class="p">])</span>
            <span class="n">heights</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">track_frame</span><span class="p">[</span><span class="s2">&quot;height&quot;</span><span class="p">])</span>

        <span class="n">trajectory</span> <span class="o">=</span> <span class="n">TrajectoryLabel</span><span class="p">(</span>
            <span class="n">timestamps</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">timestamps</span><span class="p">),</span>
            <span class="n">quaternions</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">quaternions</span><span class="p">),</span>
            <span class="n">translations</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">translations</span><span class="p">),</span>
            <span class="n">obj_class</span><span class="o">=</span><span class="n">obj_cls_idx</span><span class="p">,</span>
            <span class="n">obj_class_str</span><span class="o">=</span><span class="n">obj_cls</span><span class="p">,</span>
            <span class="n">occlusions</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">occlusions</span><span class="p">),</span>
            <span class="n">track_uuid</span><span class="o">=</span><span class="n">track_uuid</span><span class="p">,</span>
            <span class="n">log_id</span><span class="o">=</span><span class="n">json_fpath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">3</span><span class="p">],</span>
            <span class="n">max_length</span><span class="o">=</span><span class="nb">max</span><span class="p">(</span><span class="n">lengths</span><span class="p">),</span>
            <span class="n">max_width</span><span class="o">=</span><span class="nb">max</span><span class="p">(</span><span class="n">widths</span><span class="p">),</span>
            <span class="n">max_height</span><span class="o">=</span><span class="nb">max</span><span class="p">(</span><span class="n">heights</span><span class="p">),</span>
            <span class="n">lengths</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lengths</span><span class="p">),</span>
            <span class="n">widths</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">widths</span><span class="p">),</span>
            <span class="n">heights</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">heights</span><span class="p">),</span>
        <span class="p">)</span>
        <span class="n">trajectories</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trajectory</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">trajectories</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Argo AI, LLC

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>